Matrix-Vector Multiplication
This function allows you to compute matrix-vector multiplication.
An example of how to use the operation is provided below.
from csdl_om import Simulatorfrom csdl import Modelimport csdlimport numpy as npfrom scipy.sparse import csc_matrix
class ExampleMatVecProduct(Model):
def define(self): m = 3 n = 4
# Shape of the first matrix (3,2) shape1 = (m, n)
# Shape of the second matrix (2,4) shape2 = (n, )
# Creating the values of both matrices val1 = np.arange(m * n).reshape(shape1) val2 = np.arange(n).reshape(shape2)
# Declaring the input matrix and input vector mat1 = self.declare_variable('mat1', val=val1) vec1 = self.declare_variable('vec1', val=val2)
# Creating the output for matrix-vector multiplication self.register_output('MatVec', csdl.matvec(mat1, vec1))
sp = csc_matrix(mat1.val) self.register_output('SparseMatVec', csdl.matvec(sp, vec1))
sim = Simulator(ExampleMatVecProduct())sim.run()
print('mat1', sim['mat1'].shape)print(sim['mat1'])print('vec1', sim['vec1'].shape)print(sim['vec1'])print('MatVec', sim['MatVec'].shape)print(sim['MatVec'])print('SparseMatVec', sim['SparseMatVec'].shape)print(sim['SparseMatVec'])
[[ 0. 1. 2. 3.] [ 4. 5. 6. 7.] [ 8. 9. 10. 11.]]vec1 (4,)[0. 1. 2. 3.]MatVec (3,)[14. 38. 62.]SparseMatVec (3,)[14. 38. 62.]